package com.sktlab.bizconfmobile.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sktlab.bizconfmobile.R;
import com.sktlab.bizconfmobile.activity.AppClass;
import com.sktlab.bizconfmobile.model.ConfControl;
import com.sktlab.bizconfmobile.model.Participant;
import com.sktlab.bizconfmobile.util.Util;

public class ParticipantListAdapter extends BaseExpandableListAdapter {
	
	private Context mCtx;
	private ParticipantListAdapter mMe;
	private LayoutInflater mInflater;
	private List<Participant> mParticipants;
	
	public ParticipantListAdapter(Context ctx,List<Participant> data){
		
		mMe = this;
		mCtx = ctx;
		mParticipants = data;
		mInflater = (LayoutInflater)mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getGroupCount() {
		return mParticipants.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		
		return mParticipants.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		GroupHolder groupHolder = null;
		
		if(convertView == null) {
			
			groupHolder = new GroupHolder();
			
			convertView = mInflater.inflate(R.layout.item_expandable_listview_group, null);
			
			groupHolder.tvPersonName = (TextView) convertView.findViewById(R.id.tv_person_name);
			
			convertView.setTag(groupHolder);
		}else {
			
			groupHolder = (GroupHolder)convertView.getTag();
		}
		
		String name = mParticipants.get(groupPosition).getName();
		String phone = mParticipants.get(groupPosition).getPhone();
		
		do{
			
			if(!Util.isEmpty(name)) {
				
				groupHolder.tvPersonName.setText(name);
				break;
			}
			
			if(!Util.isEmpty(phone)) {
				
				groupHolder.tvPersonName.setText(name);
				break;
			}			
		}while(false);
				
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		ChildHodler childHolder = null;
		
		if(convertView == null) {
			
			childHolder = new ChildHodler();
			
			convertView = mInflater.inflate(R.layout.item_expand_listview_participant_child, null);		
			childHolder.ctvDisconnnect = (CheckedTextView) convertView.findViewById(R.id.ctv_disconnect);
			childHolder.ctvMute = (CheckedTextView) convertView.findViewById(R.id.ctv_mute);
			childHolder.ctvRename = (CheckedTextView) convertView.findViewById(R.id.ctv_rename);
			
			convertView.setTag(childHolder);
		}else {
			
			childHolder = (ChildHodler)convertView.getTag();
		}
			
		final Participant item = mParticipants.get(groupPosition);
		
		childHolder.ctvDisconnnect.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				ConfControl.getInstance().disconnectParty(item);
				mParticipants.remove(item);
				
				mMe.notifyDataSetChanged();
			}
		});
		
		
		childHolder.ctvMute.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				CheckedTextView ctv = (CheckedTextView) v;
				
				//ConfControl.getInstance().muteParty(item, state, playMessage);
				ctv.toggle();
				
				if(ctv.isChecked()) {
					
					ConfControl.getInstance().muteParty(item, 0, 0);
				}else {
					
					ConfControl.getInstance().muteParty(item, 1, 0);
				}	
			}
		});
		
		childHolder.ctvRename.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				final EditText etForInput = new EditText(mCtx);  

		        new AlertDialog.Builder(mCtx).setTitle(R.string.bt_rename)  
		                .setIcon(android.R.drawable.ic_dialog_info).setView(etForInput)  
		                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {  
		                    @Override  
		                    public void onClick(DialogInterface arg0, int arg1) {  
		                    	
		                    	do {
		                    		
		                    		if (Util.isEmpty(etForInput.getText().toString())) {
		                    			
		                    			Util.shortToast(mCtx, "name can not be null");
			                    		break;
			                    	}
		                    		
			                    	item.setName(etForInput.getText().toString());
			                    	
			                    	mMe.notifyDataSetChanged();
		                    	}while(false);
		                    	
		                    }  
		                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {

							}
						}).show();  
			}
		});
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		
		return true;
	}

	public class GroupHolder{
		
		private TextView tvPersonName;
	}
	
	public class ChildHodler{
		
		private CheckedTextView ctvDisconnnect;
		private CheckedTextView ctvMute;
		private CheckedTextView ctvRename;
	}
}
